package org.buptsse.ui;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.buptsse.listview.HomePage;
import org.buptsse.listview.ListView2Page;
import org.buptsse.listview.MyCirleListPage;
import org.buptsse.listview.MyHotListPage;


import org.buptsse.entity.User;


import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.SlidingDrawer;
import android.widget.TextView;
import android.widget.Toast;

public class MainCenterActivity extends Activity implements OnClickListener{
	// the main activity container
	private ViewPager myViewPager;
	private MyPagerAdapter myAdapter;
	private LayoutInflater mInflater;
	private List<View> mListViews;
	// the center activity tab initial
	private ViewPager mainViewPager;
	private MainPagerAdapter mainAdapter;
	
	private View layout1 = null;
	private View layout2 = null;
	
	private Button searchBack = null;
	private ImageButton search = null;
	private EditText searchText = null;
	private User user = null;

	private SlidingDrawer mDrawer;
	private Boolean flag = false;
	private ViewPager viewPager = null;

	private Button UserInfoButton,MessageButton,TradeInfoButton,HistoryButton;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main_container);
	
		
		myAdapter = new MyPagerAdapter();
		myViewPager = (ViewPager) findViewById(R.id.viewpagerLayout);
		myViewPager.setAdapter(myAdapter);

		mListViews = new ArrayList<View>();
		mInflater = getLayoutInflater();
		layout1 = mInflater.inflate(R.layout.lprofile, null);
		layout2 = mInflater.inflate(R.layout.qcenter, null);
	

		mListViews.add(layout1);
		mListViews.add(layout2);
		
		myViewPager.setCurrentItem(1);
		// 初始化第一个view的信息，个人中心
		Button back = (Button)layout1.findViewById(R.id.profile_test);
		back.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Intent it = new Intent();
				it.setClass(MainCenterActivity.this, TestActivity.class);
				MainCenterActivity.this.startActivity(it);
			}
		});
		
		UserInfoButton=(Button) layout1.findViewById(R.id.user_info_button);
		MessageButton=(Button) layout1.findViewById(R.id.message_button);
		TradeInfoButton=(Button) layout1.findViewById(R.id.trade_info_button);
		HistoryButton=(Button) layout1.findViewById(R.id.history_button);
		
		//个人资料按钮的响应函数
		UserInfoButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent =new Intent();
				intent.setClass(MainCenterActivity.this, MySettingActivity.class);
				startActivity(intent);
			}
		});
		//站内消息按钮的响应函数
		MessageButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent =new Intent();
				intent.setClass(MainCenterActivity.this, MessageAllActivity.class);
				startActivity(intent);
			}
		});
		//交易信息按钮的响应函数
		TradeInfoButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent =new Intent();
				intent.setClass(MainCenterActivity.this, TradeInfoActivity.class);
				startActivity(intent);
			}
		});
		//历史记录按钮的响应函数
		HistoryButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent =new Intent();
				intent.setClass(MainCenterActivity.this, TradeHistoryActivity.class);
				startActivity(intent);
			}
		});
		// 初始化第二个view的信息
		mainAdapter = new MainPagerAdapter(MainCenterActivity.this);
		mainViewPager = (ViewPager) layout2.findViewById(R.id.viewpagerLayout1);
		mainViewPager.setAdapter(mainAdapter);
		new ArrayList<View>();
		
		mainViewPager.setCurrentItem(0);
		// 设置第二个tab里面具体内容
		RadioGroup radioGroup = (RadioGroup) layout2
				.findViewById(R.id.rg_main_btns_qcenter);
		// here is the tab to be ture
		radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				switch (checkedId) {
				case R.id.rd_home:
					mainViewPager.setCurrentItem(0);
					break;
				case R.id.rd_near:
					mainViewPager.setCurrentItem(1);
					break;
				case R.id.rd_profile:
					mainViewPager.setCurrentItem(2);
					break;
				default:
					break;
				}
			}
		});

		
		myViewPager.setOnPageChangeListener(new OnPageChangeListener() {

			@Override
			public void onPageSelected(int arg0) {
				Log.d("k", "onPageSelected - " + arg0);
			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
				Log.d("k", "onPageScrolled - " + arg0);
			}

			@Override
			public void onPageScrollStateChanged(int arg0) {
				Log.d("k", "onPageScrollStateChanged - " + arg0);
			}
		});
		// 这里是search页面 用的是search.xml 跳转到searchresultactivity.java
		// 这里有个输入框，传参数到activity，按钮触发
		
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (requestCode == 1) {
			if (resultCode == Activity.RESULT_OK) {
				Bitmap bitmap = decodeFile(new File("/sdcard/test.jpg"));
				compress("/sdcard/tmp.jpg", bitmap);
			}
		}
		super.onActivityResult(requestCode, resultCode, data);
	}

	private Bitmap decodeFile(File f) {
		try {
			// Decode image size
			BitmapFactory.Options o = new BitmapFactory.Options();
			o.inJustDecodeBounds = true;
			BitmapFactory.decodeStream(new FileInputStream(f), null, o);
			// The new size we want to scale to
			final int REQUIRED_SIZE = 500;
			// Find the correct scale value. It should be the power of 2.
			int width_tmp = o.outWidth, height_tmp = o.outHeight;
			int scale = 1;
			while (true) {
				if (width_tmp / 2 < REQUIRED_SIZE
						|| height_tmp / 2 < REQUIRED_SIZE)
					break;
				width_tmp /= 2;
				height_tmp /= 2;
				scale *= 2;
			}
			// Decode with inSampleSize
			BitmapFactory.Options o2 = new BitmapFactory.Options();
			o2.inSampleSize = scale;
			return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
		} catch (FileNotFoundException e) {
			return null;
		}
	}

	private void compress(String filename, Bitmap bitmap) {
		File file = new File(filename);
		if (file.exists()) {
			file.delete();
		}
		try {
			FileOutputStream out = new FileOutputStream(file);
			if (bitmap.compress(Bitmap.CompressFormat.JPEG, 60, out)) {
				out.flush();
				out.close();
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	// 设置菜单
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.menu, menu);
		return true;
	}

	public boolean onOptionsItemSelected(MenuItem item) {
		// 得到当前选中的MenuItem的ID,
		int item_id = item.getItemId();

//		switch (item_id) {
//		case R.id.acount_menu:
//			Intent it = new Intent(MainCenterActivity.this,
//					AcountActivity.class);
//			it.putExtra("user", user);
//			MainCenterActivity.this.startActivity(it);
//			break;
//		case R.id.call_back_menu:
//			it = new Intent(MainCenterActivity.this, AboutActivity.class);
//			it.putExtra("user", user);
//			MainCenterActivity.this.startActivity(it);
//			break;
//		case R.id.setting_menu:
//			it = new Intent(MainCenterActivity.this, QMySettingActivity.class);
//			it.putExtra("user", user);
//			MainCenterActivity.this.startActivity(it);
//			break;
//		case R.id.about_menu:
//			it = new Intent(MainCenterActivity.this, QMyJouneryListActivity.class);
//			it.putExtra("user", user);
//			MainCenterActivity.this.startActivity(it);
//			break;
//		case R.id.quit_menu:
//			this.finish();
//			break;
//		}
		return true;
	}

	// 菜单完毕

	private class MyPagerAdapter extends PagerAdapter {

		@Override
		public void destroyItem(View arg0, int arg1, Object arg2) {
			Log.d("k", "destroyItem");
			((ViewPager) arg0).removeView(mListViews.get(arg1));
		}

		@Override
		public void finishUpdate(View arg0) {
			Log.d("k", "finishUpdate");
		}

		@Override
		public int getCount() {
			Log.d("k", "getCount");
			return mListViews.size();
		}

		@Override
		public Object instantiateItem(View arg0, int arg1) {
			Log.d("k", "instantiateItem");
			((ViewPager) arg0).addView(mListViews.get(arg1), 0);
			return mListViews.get(arg1);
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			Log.d("k", "isViewFromObject");
			return arg0 == (arg1);
		}

		@Override
		public void restoreState(Parcelable arg0, ClassLoader arg1) {
			Log.d("k", "restoreState");
		}

		@Override
		public Parcelable saveState() {
			Log.d("k", "saveState");
			return null;
		}

		@Override
		public void startUpdate(View arg0) {
			Log.d("k", "startUpdate");
		}

	}

	private class MainPagerAdapter extends PagerAdapter {

		private ArrayList<LinearLayout> views;

		public MainPagerAdapter(Context context) {
			views = new ArrayList<LinearLayout>();
			views.add(new MyHotListPage(context,user));// Hot页面
			views.add(new MyCirleListPage(context,user));// 圈子列表
			views.add(new HomePage(context,user));
		}

		@Override
		public void destroyItem(View view, int arg1, Object object) {
			((ViewPager) view).removeView((LinearLayout) object);
		}

		@Override
		public void finishUpdate(View arg0) {

		}

		@Override
		public int getCount() {
			return views.size();
		}

		@Override
		public Object instantiateItem(View view, int position) {
			View myView = views.get(position);
			((ViewPager) view).addView(myView);
			return myView;
		}

		@Override
		public boolean isViewFromObject(View view, Object object) {
			return view == object;
		}

		@Override
		public void restoreState(Parcelable arg0, ClassLoader arg1) {

		}

		@Override
		public Parcelable saveState() {
			return null;
		}

		@Override
		public void startUpdate(View arg0) {
		}
	}

	

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		int vid = v.getId();
		Intent it = new Intent();
		switch(vid){
//		case R.id.top_back:
//			//it.putExtra("user", user);
//			MainCenterActivity.this.setResult(RESULT_OK, it);
//			MainCenterActivity.this.finish();
//			break;
//		case R.id.search:
//			if(check()){
//				//it.setClass(MainCenterActivity.this, QSearchActivity.class);
//				//it.putExtra("user", user);
//				it.putExtra("content", searchText.getText().toString());
//				MainCenterActivity.this.startActivity(it);
//				break;
//			}
		}
	}
	private boolean check(){
		String content = searchText.getText().toString();
		if(null==content||"".equals(content))
			return false;
		return true;
	}

}